/**
 * @ProjectName: ShopSytem
 * @Title: OrderDaoImpl.java 
 * @Package com.element5th.shop.dao.order 
 * @Description: 前台订单相关数据库操作
 * @author 第五元素小组   
 * @date 2012-10-26 15:40:02 
 * @version V1.0   
 */
package com.element5th.shop.dao.order;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.element5th.shop.comm.BaseDao;
import com.element5th.shop.comm.SystemException;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.dto.OrderFindDto;
import com.element5th.shop.entity.OrderList;
import com.element5th.shop.entity.OrderPaytype;
import com.element5th.shop.entity.ShopOrder;
import com.element5th.shop.entity.User;

/**
 * The Class OrderDaoImpl.
 */
public class OrderDaoImpl extends BaseDao implements IOrderDao {

	/*
	 * Title: orderList Description: 查看用户订单列表,返回订单列表
	 * 
	 * @param dto 用户对象
	 * 
	 * @return 包含订单列表的用户对象
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#orderList(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public List<BaseDto> orderList() {
		this.openConnection();
		List<ShopOrder> list = this.findAll(ShopOrder.class);
		List<BaseDto> listDto = new ArrayList<BaseDto>();
		for (ShopOrder order : list) {
			BaseDto dto = new BaseDto();
			//用于查询用户名称
			User user = this.findById(User.class, order.getUserId());
			dto.setUser(user);
			OrderPaytype orderpaytype = this.findById(OrderPaytype.class, order.getPaytypeId());
			dto.setOrderPayType(orderpaytype);
			//用于查询订单内的商品名称
			List<OrderList> orderList = this.findListByCol(OrderList.class, 
					"orderId", order.getOrderId());
			order.setOrderLists(orderList);
			dto.setShopOrder(order);
			listDto.add(dto);
		}
		this.commit();
		return listDto;
	}

	/*
	 * Title: canelOrder Description: 取消订单
	 * 
	 * @param dto 订单对象
	 * 
	 * @return 操作是否成功
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#canelOrder(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public boolean canelOrder(BaseDto dto) {
		return false;
	}

	/*
	 * Title: updateOrder Description: 更新订单
	 * 
	 * @param dto 订单对象
	 * 
	 * @return 操作是否成功
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#updateOrder(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public boolean updateOrder(BaseDto dto) {
		return false;
	}

	/*
	 * Title: goodReview Description: 对购买商品进行评价
	 * 
	 * @param dto 评价信息对象
	 * 
	 * @return 操作是否成功
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#goodReview(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public boolean goodReview(BaseDto dto) {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * Title: orderRecord Description: 生成订单报表
	 * 
	 * @param orderDto 订单查询条件
	 * 
	 * @param response 响应对象，用于向客户端返回报表文件
	 * 
	 * @return 操作是否成功
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#orderRecord(com.element5th.shop
	 * .dto.OrderFindDto, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public boolean orderRecord(OrderFindDto orderDto,
			HttpServletResponse response) {
		return false;
	}

	/*
	 * Title: orderDetail Description: 查看订单详情
	 * 
	 * @param dto 订单对象
	 * 
	 * @return 包含订单明细列表的订单对象
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#orderDetail(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public BaseDto orderDetail(BaseDto dto) {
		this.openConnection();

		
		return null;
	}

	/*
	 * Title: addOrder Description: 提交订单
	 * 
	 * @param dto 包含订单明细列表的订单对象
	 * 
	 * @return 操作是否成功request.getparameter("orderDiscount");
	 * 
	 * @see
	 * com.element5th.shop.dao.order.IOrderDao#addOrder(com.element5th.shop.
	 * dto.BaseDto)
	 */
	@Override
	public boolean addOrder(BaseDto dto) throws SystemException {
		boolean flag;
		this.openConnection();
		flag = this.save(dto.getShopOrder());
		System.out.println(flag);
		if (!flag) {
			throw new SystemException(2001, "插入数据库失败！");
		}
		this.commit();
		return flag;
	}

}
